package com.ciwong.epaper.modules.epaper.ui;

import android.content.Intent;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.cordva.BaseHtmlActicity;
import com.ciwong.epaper.modules.cordva.SubmitEvent;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.eventbus.EventBus;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.google.gson.Gson;

/**
 * @description:互动教程（跳转网页）
 * @author: wyk
 * @time: 2021/6/3 18:39
 */
public class InteractiveTutorialDetaisActivity extends BaseHtmlActicity {
    private static final String TAG = "InteractiveTutorialDetaisActivity";
    private String title, versionId, packageId;
    private int contentId = 0;
    private int index;
    private String cId;
    Gson gson = new Gson();

    @Override
    protected void preCreate() {
        super.preCreate();
        Intent intent = getIntent();
        title = intent.getStringExtra(IntentFlag.INTENT_FLAG_TITLE);
        versionId = intent.getStringExtra(IntentFlag.INTENT_FLAG_VERSION_ID);
        packageId = intent.getStringExtra(IntentFlag.INTENT_FLAG_BOOKID);
        cId = intent.getStringExtra(IntentFlag.INTENT_FLAG_CID);
    }


    @Override
    protected void init() {
        super.init();
        setValideSource(false);
        //if (null != title)
        setTitleText("作业详情");
        //setTitleText(title);

        BaseRequest.VerifyInfo verifyInfo = HttpRequest.getVerifyInfo();
        if (null != verifyInfo) {
            String clientId = verifyInfo.getClientId();
            String userId = EApplication.getInstance().getUserInfoBase().getUserId() + "";
            String brandId = EApplication.BRAND_ID + "";
            String startURL = EpaperConstant.URL_H5_INTERACTIVE_TUTORIAL + "clientId=" + clientId + "&versionId=" + versionId + "&brandId=" + brandId + "&contentId=" + contentId + "&packageId=" + packageId + "&cId=" + cId;
            setStartURL(startURL);
            loadUrl(startURL.toString());
        } else {
            showToastError(getString(R.string.get_user_info_error));
            finish();
        }
    }


    @Override
    protected void loadData() {
    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void finish() {
        if (webView.canGoBack()) {
            webView.goBack();
            return;
        }
        super.finish();
    }


    public void onEventMainThread(SubmitEvent submitEvent) {
        CWLog.d(TAG, "onEventMainThread+" + submitEvent.getId() + "--Json:" + submitEvent.getJson());
        if (submitEvent.getId().equals("InteractiveTutorialDetaisActivity")) {//
            /*JumpServiceParamBean jumpServiceParaBean = gson.fromJson(submitEvent.getJson(), JumpServiceParamBean.class);
            int serviceId = jumpServiceParaBean.getServiceId();
            String urlH5 = jumpServiceParaBean.getUrl();*/
            jumpToInnerResouce(submitEvent.getJson());
        }
    }

    /**
     * 跳入h5内部资源
     *
     * @param urlH5
     */
    public void jumpToInnerResouce(String urlH5) {
        if (!urlH5.contains("http")) {//解决连接中不包含http 导致闪退的bug
            urlH5 = "http://" + urlH5;
        }
        MeJumpManager.jumpToResourseCenterInnerH5Activity(R.string.go_back, this, urlH5);

    }
}
